/* ******************************************************************
 * This file is part of the escal project and distributed under the
 * terms of the GNU General Public License Version 3
 * CopyRight (C) 2008 Epocher (xijiaosdq@gmail.com)
 * See COPYING file for licensing information.
 *******************************************************************/

#include "escal_gui.h"
#include "escal.h"
#include <iostream>
using namespace Escal;


EscalGui::EscalGui(QWidget* parent): QMainWindow(parent)
{
	setupUi(this);
	resize(settings->value("MainWindowSize").toSize());
	/*
	 * we may not need e_UrlDialog, so we set it to NULL
	 * until the first time we use it
	 */
	urlDialog=NULL;
	/*
	 * initialize the task views
	 */
	undoneTaskModel=new UndoneTaskModel(this);
	undoneTaskModel->loadTaskList();
	connect(undoneTaskModel, SIGNAL(taskCompleted(BaseTask *)),
		this, SLOT(taskCompleted(BaseTask *)));
	connect(undoneTaskModel, SIGNAL(taskDeleted(BaseTask *)),
		this, SLOT(taskDeleted(BaseTask *)));
	undoneTaskView->setModel(undoneTaskModel);
	undoneTaskView->setSelectionMode(QAbstractItemView::ExtendedSelection);
	undoneTaskView->setSelectionBehavior(QAbstractItemView::SelectRows);
	undoneTaskView->setRootIsDecorated(false);
	undoneTaskView->setSortingEnabled(true);
	undoneTaskViewDelegate = new UndoneTaskViewDelegate(this);
	undoneTaskView->setItemDelegate(undoneTaskViewDelegate);
	

	doneTaskModel=new DoneTaskModel(this);
	doneTaskModel->loadTaskList();
	connect(doneTaskModel, SIGNAL(taskDeleted(BaseTask *)),
		this, SLOT(taskDeleted(BaseTask *)));
	doneTaskView->setModel(doneTaskModel);
	doneTaskView->setSelectionMode(QAbstractItemView::ExtendedSelection);
	doneTaskView->setSelectionBehavior(QAbstractItemView::SelectRows);
	doneTaskView->setRootIsDecorated(false);
	doneTaskView->setSortingEnabled(true);

	folderListModel=new FolderListModel(this);
	connect(settings, SIGNAL(foldersChanged()),
		folderListModel, SLOT(foldersChanged()));
	folderView->setModel(folderListModel);
	
	dirModel = new QDirModel(this);

	connect(folderView, SIGNAL(clicked(const QModelIndex &)),
		this, SLOT(folderClicked(const QModelIndex &)));
	/*
	 * connect gui signals 
	 */
	connect(actionAboutQt, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
	connect(qApp, SIGNAL(lastWindowClosed()), this, SLOT(quit()));

		
}
EscalGui::~EscalGui(){
	if(urlDialog!=NULL) delete urlDialog;
}

void EscalGui::close(){
	quit();
	QMainWindow::close();
};

void EscalGui::quit(){
	qDebug() << "EscalGui::quit()";
	settings->setValue("MainWindowSize", size());
	undoneTaskModel->stopTask();
	undoneTaskModel->saveTaskList();
	doneTaskModel->saveTaskList();
}

void EscalGui::about(){
};
void EscalGui::aboutQt(){
};
void EscalGui::newTask(){
	if(urlDialog==NULL) urlDialog=new UrlDialog();
	urlDialog->reset();
	for(;;){
		if(urlDialog->exec() != QDialog::Accepted) 
			return;
		QUrl url = urlDialog->getUrl();
		QString fileName = urlDialog->getFileName();
		QString folderName = urlDialog->getFolderName();
		int threadNum = urlDialog->getThreadNum();
		QDir dir(folderName);
		if(!dir.exists() && !dir.mkdir(folderName)){
			QMessageBox::warning(this, tr("warning"),
				tr("create directory failed!"));
			continue;
		}
		if(dir.exists(fileName)){
			for(int i=1;;i++){
				if(!dir.exists(QString("%1.%2")
					.arg(fileName).arg(i)))
				{
					fileName = QString("%1.%2")
					.arg(fileName).arg(i);
					break;
				}
			}
		}
		QFile file(dir.filePath(fileName));
		file.open(QIODevice::WriteOnly);
		file.close();
		TaskFactory factory;
		BaseTask *task=factory.createNewTask(url, folderName, 
			fileName , threadNum, this);
		if(task==NULL){
			QMessageBox::critical(this, tr("error"),
				tr("fail to create a task"));
			return;
		}	
		undoneTaskModel->addTask(task);
		break;
	}
	//task->start();

};

void EscalGui::startSelected(){
	QItemSelectionModel *selectionModel = undoneTaskView->selectionModel();
	QModelIndexList indexes = selectionModel->selectedRows();
	QModelIndex index;

	foreach(index, indexes) {
		std::cout << index.row() << " " << index.column() << std::endl;
		undoneTaskModel->startTask(index);
	}
};

void EscalGui::pauseSelected(){
	QItemSelectionModel *selectionModel = undoneTaskView->selectionModel();
	QModelIndexList indexes = selectionModel->selectedRows();
	QModelIndex index;

	foreach(index, indexes) {
		//std::cout << index.row() << " " << index.column() << std::endl;
		undoneTaskModel->pauseTask(index);
	}

};
void EscalGui::stopSelected(){
	QItemSelectionModel *selectionModel = undoneTaskView->selectionModel();
	QModelIndexList indexes = selectionModel->selectedRows();
	QModelIndex index;

	foreach(index, indexes) {
		//std::cout << index.row() << " " << index.column() << std::endl;
		undoneTaskModel->stopTask(index);
	}

};

void EscalGui::increaseSelected(){
	QItemSelectionModel *selectionModel = undoneTaskView->selectionModel();
	QModelIndexList indexes = selectionModel->selectedRows();
	QModelIndex index;

	foreach(index, indexes) {
		//std::cout << index.row() << " " << index.column() << std::endl;
		undoneTaskModel->increaseTask(index);
	}

};
void EscalGui::cutDownSelected(){
	QItemSelectionModel *selectionModel = undoneTaskView->selectionModel();
	QModelIndexList indexes = selectionModel->selectedRows();
	QModelIndex index;

	foreach(index, indexes) {
		//std::cout << index.row() << " " << index.column() << std::endl;
		undoneTaskModel->cutDownTask(index);
	}

};


void EscalGui::cancelSelected(){
	QItemSelectionModel *selectionModel = undoneTaskView->selectionModel();
	QModelIndexList indexes = selectionModel->selectedRows();
	undoneTaskModel->deleteTasks(indexes);

};

void EscalGui::preference(){

};

void EscalGui::taskCompleted(BaseTask *task){
	doneTaskModel->addTask(task);
};

void EscalGui::taskDeleted(BaseTask *task){
	delete task;
};

void EscalGui::undoneTaskClicked(const QModelIndex & index){
	
};

void EscalGui::doneTaskClicked(const QModelIndex & index){
	
};

void EscalGui::folderClicked(const QModelIndex & index){
	infoView->setModel(dirModel);
	QString folderPath = index.model()->data(index).toString();
	infoView->setRootIndex(dirModel->index(folderPath));
};

